import ShooterPlant from "./plant/ShooterPlant.js";

export class Domain {
    constructor(scene, x, y, width, height, color, clickCallback) {
        this.scene = scene;
        this.width = width;
        this.height = height;
        this.use = false
        this.x = x;
        this.y = y;

        // 创建一个Graphics对象用于绘制长方形
        this.graphics = this.scene.add.graphics();
        this.graphics.fillStyle(color, 0);
        this.graphics.fillRect(x, y, width, height);

        // 创建一个可用于事件检测的区域
        this.rectangle = new Phaser.Geom.Rectangle(x, y, width, height);

        // 添加事件监听
        this.graphics.setInteractive(this.rectangle, Phaser.Geom.Rectangle.Contains);

        // 监听鼠标悬浮事件
        this.graphics.on('pointerover', () => {
            if (this.use) return
            this.onHover();
        });

        // 监听鼠标离开事件
        this.graphics.on('pointerout', () => {
            if (this.use) return
            this.onOut();
        });

        // 监听鼠标点击事件
        this.graphics.on('pointerdown', () => {
            if (this.use) return
            this.onClick();
        });

        // 加载图片
        this.image = this.scene.add.image(x + width / 2, y + height / 2, "Peashooter0");
        this.image.setAlpha(0.5); // 设置透明度为50%
        this.image.setOrigin(0.5, 0.5); // 将图片原点设置在中心
        this.image.setVisible(false); // 初始时隐藏图片

        // 保存外部点击回调
        this.clickCallback = clickCallback;
    }

    onHover() {
        // 加载图片
        if (this.scene.cur_card) {
            this.image.setTexture(this.scene.cur_card);
            this.image.setVisible(true);
        }
    }

    onOut() {
        if (this.scene.cur_card) {
            this.image.setVisible(false);
        }
    }

    onClick() {
        console.log('Rectangle clicked!');

        if (this.scene.cur_card) {
            this.image.setVisible(false);
        }

        // 调用外部传入的点击回调（如果存在）
        if (this.clickCallback) {
            this.use = true
            this.clickCallback(this.x + this.width / 2, this.y + this.height / 2 - 15);
        }

        // 执行点击时的逻辑
    }
}